package com.readbook.action;


import com.alibaba.fastjson.JSONArray;
import com.readbook.service.SjglService;
import com.readbook.util.ExcelUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/sjgl")
public class sjgl {
    @Resource
    private SjglService sjglService;


    @RequestMapping("getChildClass.html")
    @ResponseBody
    public List<Map<String, Object>> getChildClass(HttpServletRequest request, HttpServletResponse response) {
        String pid = request.getParameter("pid").toString().trim();
        List<Map<String, Object>> list1 = sjglService.getChildClass(pid);
        return list1;
    }

    @RequestMapping("Search_book.html")
    @ResponseBody
    public Map<String, Object> Search_book(HttpServletRequest request, HttpServletResponse response) {
        String pid = "";
        if (request.getParameter("pid") != null) {
            pid = request.getParameter("pid").toString().trim();
        }

        String classid = "";
        if (request.getParameter("classid") != null) {
            classid = request.getParameter("classid").toString().trim();
        }

        String bookname = "";
        if (request.getParameter("bookname") != null) {
            bookname = request.getParameter("bookname").toString().trim();
        }


        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author").toString().trim();
        }

        String tjly = "";
        if (request.getParameter("tjly") != null) {
            tjly = request.getParameter("tjly").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("pid", pid);
        gmap.put("classid", classid);
        gmap.put("bookname", bookname);
        gmap.put("author", author);
        gmap.put("tjly", tjly);
        List<Map<String, Object>> list1 = sjglService.Search_book(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    @RequestMapping("del_book.html")
    @ResponseBody
    public Map<String, Object> del_book(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid").trim();
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        Map<String, Object> getm = sjglService.del_book(gmap);
        return getm;
    }


    /**
     * @author 牟明艳
     * @comments 书籍添加
     * @time 2019年8月7日10:11:16
     */
    @RequestMapping("InsertBook.html")
    public @ResponseBody
    Map<String, Object> InsertBook(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String bid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("bid", bid);

        //分类id
        String classid = "";
        if (req.getParameter("classid") != null) {
            classid = req.getParameter("classid").toString().trim();
        }
        gmap.put("classid", classid);

        //书名
        String bookname = "";
        if (req.getParameter("bookname") != null) {
            bookname = req.getParameter("bookname").toString().trim();
        }
        gmap.put("bookname", bookname);

        //总页数
        String totalpage = "";
        if (req.getParameter("totalpage") != null) {
            totalpage = req.getParameter("totalpage").toString().trim();
        }
        gmap.put("totalpage", totalpage);


        //每天最低读书量
        String minnum = "";
        if (req.getParameter("minnum") != null) {
            minnum = req.getParameter("minnum").toString().trim();
        }
        gmap.put("minnum", minnum);

        //封面
        String fm = "";
        if (req.getParameter("fm") != null) {
            fm = req.getParameter("fm").toString().trim();
        }
        gmap.put("fm", fm);

        //推荐阅读年级
        String nj = "";
        if (req.getParameter("nj") != null) {
            nj = req.getParameter("nj").toString().trim();
        }
        gmap.put("nj", nj);

        //推荐阅读班级
        String bj = "";
        if (req.getParameter("bj") != null) {
            bj = req.getParameter("bj").toString().trim();
        }
        gmap.put("bj", bj);

        //作者
        String author = "";
        if (req.getParameter("author") != null) {
            author = req.getParameter("author").toString().trim();
        }
        gmap.put("author", author);

        //推荐理由
        String reason = "";
        if (req.getParameter("reason") != null) {
            reason = req.getParameter("reason").toString().trim();
        }
        gmap.put("reason", reason);

        //书籍简介
        String bookjj = "";
        if (req.getParameter("bookjj") != null) {
            bookjj = req.getParameter("bookjj").toString().trim();
        }
        gmap.put("bookjj", bookjj);

        //作者简介
        String authorjj = "";
        if (req.getParameter("authorjj") != null) {
            authorjj = req.getParameter("authorjj").toString().trim();
        }
        gmap.put("authorjj", authorjj);

        //书籍基本信息
        String bookxx = "";
        if (req.getParameter("bookxx") != null) {
            bookxx = req.getParameter("bookxx").toString().trim();
        }
        gmap.put("bookxx", bookxx);

        //书籍总字数
        String words = "";
        if (req.getParameter("words") != null) {
            words = req.getParameter("words").toString().trim();
        }
        gmap.put("words", words);

        gmap.put("sh", "0");

        HttpSession session = ((HttpServletRequest) req).getSession();
        if (session.getAttribute("usid") != null) {
            gmap.put("recommendorid", session.getAttribute("usid").toString());
        }

        if (session.getAttribute("name") != null) {
            gmap.put("recommendor", session.getAttribute("name").toString());
        }
        gmap.put("tjly", "0");//推荐来源

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("addtime", df.format(new Date()));


        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = sjglService.InsertBook(gmap);
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 书籍修改
     * @time 2019年8月7日10:11:16
     */
    @RequestMapping("EditBook.html")
    public @ResponseBody
    Map<String, Object> EditBook(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String bid = req.getParameter("bid");
        gmap.put("bid", bid);

        //分类id
        String classid = "";
        if (req.getParameter("classid") != null) {
            classid = req.getParameter("classid").toString().trim();
        }
        gmap.put("classid", classid);

        //书名
        String bookname = "";
        if (req.getParameter("bookname") != null) {
            bookname = req.getParameter("bookname").toString().trim();
        }
        gmap.put("bookname", bookname);

        //起页
        String startnum = "";
        if (req.getParameter("startnum") != null) {
            startnum = req.getParameter("startnum").toString().trim();
        }
        gmap.put("startnum", startnum);

        //止页
        String endnum = "";
        if (req.getParameter("endnum") != null) {
            endnum = req.getParameter("endnum").toString().trim();
        }
        gmap.put("endnum", endnum);

        //所需时长(分)
        String needtime = "";
        if (req.getParameter("needtime") != null) {
            needtime = req.getParameter("needtime").toString().trim();
        }
        gmap.put("needtime", needtime);

        //每天最低读书量
        String minnum = "";
        if (req.getParameter("minnum") != null) {
            minnum = req.getParameter("minnum").toString().trim();
        }
        gmap.put("minnum", minnum);

        //封面
        String fm = "";
        if (req.getParameter("fm") != null) {
            fm = req.getParameter("fm").toString().trim();
        }
        gmap.put("fm", fm);

        //阅读完后可获取的总积分
        String jf = "";
        if (req.getParameter("jf") != null) {
            jf = req.getParameter("jf").toString().trim();
        }
        gmap.put("jf", jf);

        //推荐阅读年级
        String nj = "";
        if (req.getParameter("nj") != null) {
            nj = req.getParameter("nj").toString().trim();
        }
        gmap.put("nj", nj);

        //推荐阅读班级
        String bj = "";
        if (req.getParameter("bj") != null) {
            bj = req.getParameter("bj").toString().trim();
        }
        gmap.put("bj", bj);

        //作者
        String author = "";
        if (req.getParameter("author") != null) {
            author = req.getParameter("author").toString().trim();
        }
        gmap.put("author", author);

        //推荐理由
        String reason = "";
        if (req.getParameter("reason") != null) {
            reason = req.getParameter("reason").toString().trim();
        }
        gmap.put("reason", reason);

        //简介
        String bookjj = "";
        if (req.getParameter("bookjj") != null) {
            bookjj = req.getParameter("bookjj").toString().trim();
        }
        gmap.put("bookjj", bookjj);


        //作者简介
        String authorjj = "";
        if (req.getParameter("authorjj") != null) {
            authorjj = req.getParameter("authorjj").toString().trim();
        }
        gmap.put("authorjj", authorjj);

        //书籍基本信息
        String bookxx = "";
        if (req.getParameter("bookxx") != null) {
            bookxx = req.getParameter("bookxx").toString().trim();
        }
        gmap.put("bookxx", bookxx);

        //总字数
        String words = "";
        if (req.getParameter("words") != null) {
            words = req.getParameter("words").toString().trim();
        }
        gmap.put("words", words);


        //总字数
        String totalpage = "";
        if (req.getParameter("totalpage") != null) {
            totalpage = req.getParameter("totalpage").toString().trim();
        }
        gmap.put("totalpage", totalpage);

        String sh = "";
        if (req.getParameter("sh") != null) {
            sh = req.getParameter("sh");
        }
        gmap.put("sh", sh);

        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = sjglService.EditBook(gmap);
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "修改失败,请联系管理员");
        return rmap;
    }


    @RequestMapping("setshzt.html")
    public @ResponseBody
    void setshzt(HttpServletRequest req) {
        String sh = req.getParameter("sh");
        String bid = req.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("sh", sh);
        gmap.put("bid", bid);
        sjglService.EditBook(gmap);
    }


    @RequestMapping("booksetsfsj.html")
    public @ResponseBody
    void booksetsfsj(HttpServletRequest req) {
        String sfsj = req.getParameter("sfsj");
        String bid = req.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("sfsj", sfsj);
        gmap.put("bid", bid);
        sjglService.EditBook(gmap);
    }


    @RequestMapping("import_Booktm.html")
    @ResponseBody
    public Boolean import_Booktm(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String bid = request.getParameter("bid");

        List<Map<String, Object>> lsitdata = ExcelUtils.getExceldatas(request);
        int x = 0;
        for (int i = 0; i < lsitdata.size(); i++) {
            Map<String, Object> maps = lsitdata.get(i);
            String tmnr = "";
            if (maps.get("题目内容") != null) {
                tmnr = maps.get("题目内容").toString().trim();
            } else {
                continue;
            }

            String tx = "";
            if (maps.get("题型") != null) {
                String tixing = maps.get("题型").toString().trim();
                if (tixing.equals("选择题")) {
                    tx = "1";
                } else {
                    tx = "0";
                }
            }

            String answer = "";
            if (maps.get("答案") != null) {
                answer = maps.get("答案").toString().trim();
            }
            String qid = UUID.randomUUID().toString().replaceAll("-", "");


            //答案 ABCDEF
            Map<String, Object> gmap = new HashMap<>();
            gmap.put("tmnr", tmnr);
            gmap.put("tx", tx);
            gmap.put("answer", answer);
            gmap.put("qid", qid);
            gmap.put("bid", bid);

            //保存选项
            if (maps.get("A") != null) {
                String a1 = maps.get("A").toString();
                gmap.put("AD", a1);
            }

            if (maps.get("B") != null) {
                String b1 = maps.get("B").toString();
                gmap.put("BD", b1);
            }

            if (maps.get("C") != null) {
                String c1 = maps.get("C").toString();
                gmap.put("CD", c1);
            }

            if (maps.get("D") != null) {
                String d1 = maps.get("D").toString();
                gmap.put("DD", d1);
            }

            if (maps.get("E") != null) {
                String e1 = maps.get("E").toString();
                gmap.put("ED", e1);
            }

            if (maps.get("F") != null) {
                String f1 = maps.get("F").toString();
                gmap.put("FD", f1);
            }
            int m = sjglService.insert_bookquestion(gmap);

            if (m > 0) {
                x++;
            }
        }

        if (x > 0) {
            String filepath = request.getParameter("filepath").toString();
            String realpath = request.getRealPath(filepath);
            File srcFile = new File(realpath);
            if (srcFile.exists()) {
                srcFile.delete();
            }
            return true;
        } else {
            return false;
        }
    }


    @RequestMapping("Search_Tm.html")
    @ResponseBody
    public Map<String, Object> Search_Tm(HttpServletRequest request, HttpServletResponse response) {
        String bid = "";
        if (request.getParameter("bid") != null) {
            bid = request.getParameter("bid").toString().trim();
        }
        Map<String, Object> gmap = new HashMap<>();
        gmap.put("bid", bid);
        List<Map<String, Object>> list1 = sjglService.Search_Tm(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    @RequestMapping("InsertBookQuestion.html")
    @ResponseBody
    public Map<String, Object> InsertBookQuestion(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String qid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("qid", qid);

        //题目内容
        String tmnr = "";
        if (request.getParameter("tmnr") != null) {
            tmnr = request.getParameter("tmnr").toString().trim();
        }
        gmap.put("tmnr", tmnr);

        //题型
        String tx = "";
        if (request.getParameter("tx") != null) {
            tx = request.getParameter("tx").toString().trim();
        }
        gmap.put("tx", tx);

        //答案
        String answer = "";
        if (request.getParameter("answer") != null) {
            answer = request.getParameter("answer").toString().trim();
        }
        gmap.put("answer", answer);

        //书籍id
        String bid = "";
        if (request.getParameter("bid") != null) {
            bid = request.getParameter("bid").toString().trim();
        }
        gmap.put("bid", bid);

        //A答案
        String ad = "";
        if (request.getParameter("ad") != null) {
            ad = request.getParameter("ad").toString().trim();
        }
        gmap.put("AD", ad);

        //B答案
        String bd = "";
        if (request.getParameter("bd") != null) {
            bd = request.getParameter("bd").toString().trim();
        }
        gmap.put("BD", bd);

        //C答案
        String cd = "";
        if (request.getParameter("cd") != null) {
            cd = request.getParameter("cd").toString().trim();
        }
        gmap.put("CD", cd);

        //D答案
        String dd = "";
        if (request.getParameter("dd") != null) {
            dd = request.getParameter("dd").toString().trim();
        }
        gmap.put("DD", dd);

        //E答案
        String ed = "";
        if (request.getParameter("ed") != null) {
            ed = request.getParameter("ed").toString().trim();
        }
        gmap.put("ED", ed);

        //F答案
        String fd = "";
        if (request.getParameter("fd") != null) {
            fd = request.getParameter("fd").toString().trim();
        }
        gmap.put("FD", fd);

        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = sjglService.insert_bookquestion(gmap);
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


    @RequestMapping("EditBookQuestion.html")
    @ResponseBody
    public Map<String, Object> EditBookQuestion(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String qid = request.getParameter("qid");
        gmap.put("qid", qid);

        //题目内容
        String tmnr = "";
        if (request.getParameter("tmnr") != null) {
            tmnr = request.getParameter("tmnr").toString().trim();
        }
        gmap.put("tmnr", tmnr);

        //题型
        String tx = "";
        if (request.getParameter("tx") != null) {
            tx = request.getParameter("tx").toString().trim();
        }
        gmap.put("tx", tx);

        //答案
        String answer = "";
        if (request.getParameter("answer") != null) {
            answer = request.getParameter("answer").toString().trim();
        }
        gmap.put("answer", answer);

        //A答案
        String ad = "";
        if (request.getParameter("ad") != null) {
            ad = request.getParameter("ad").toString().trim();
        }
        gmap.put("AD", ad);

        //B答案
        String bd = "";
        if (request.getParameter("bd") != null) {
            bd = request.getParameter("bd").toString().trim();
        }
        gmap.put("BD", bd);

        //C答案
        String cd = "";
        if (request.getParameter("cd") != null) {
            cd = request.getParameter("cd").toString().trim();
        }
        gmap.put("CD", cd);

        //D答案
        String dd = "";
        if (request.getParameter("dd") != null) {
            dd = request.getParameter("dd").toString().trim();
        }
        gmap.put("DD", dd);

        //E答案
        String ed = "";
        if (request.getParameter("ed") != null) {
            ed = request.getParameter("ed").toString().trim();
        }
        gmap.put("ED", ed);

        //F答案
        String fd = "";
        if (request.getParameter("fd") != null) {
            fd = request.getParameter("fd").toString().trim();
        }
        gmap.put("FD", fd);

        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = sjglService.EditBookQuestion(gmap);
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "修改失败,请联系管理员");
        return rmap;
    }


    @RequestMapping("del_bookquestion.html")
    @ResponseBody
    public Map<String, Object> del_bookquestion(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String qid = request.getParameter("qid");
        gmap.put("qid", qid);
        int m = sjglService.del_bookquestion(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "删除成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "删除失败,请联系管理员");
        return rmap;
    }


    @RequestMapping("Search_bookBySh.html")
    @ResponseBody
    public Map<String, Object> Search_bookBySh(HttpServletRequest request, HttpServletResponse response) {
        String pid = "";
        if (request.getParameter("pid") != null) {
            pid = request.getParameter("pid").toString().trim();
        }

        String classid = "";
        if (request.getParameter("classid") != null) {
            classid = request.getParameter("classid").toString().trim();
        }

        String bookname = "";
        if (request.getParameter("bookname") != null) {
            bookname = request.getParameter("bookname").toString().trim();
        }


        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author").toString().trim();
        }

        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj").toString().trim();
        }

        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj").toString().trim();
        }

        String tjly = "";
        if (request.getParameter("tjly") != null) {
            tjly = request.getParameter("tjly").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("pid", pid);
        gmap.put("classid", classid);
        gmap.put("bookname", bookname);
        gmap.put("author", author);
        gmap.put("nj", nj);
        gmap.put("bj", bj);
        gmap.put("tjly", tjly);
        List<Map<String, Object>> list1 = sjglService.Search_bookBySh(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    @RequestMapping("Search_XdthByBid.html")
    @ResponseBody
    public Map<String, Object> Search_XdthByBid(HttpServletRequest request, HttpServletResponse response) {
        String keyword = "";
        if (request.getParameter("keyword") != null) {
            keyword = request.getParameter("keyword").toString().trim();
        }

        String bid = "";
        if (request.getParameter("bid") != null) {
            bid = request.getParameter("bid").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("keyword", keyword);
        gmap.put("bid", bid);
        List<Map<String, Object>> list1 = sjglService.Search_XdthByBid(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    @RequestMapping("xdthSh.html")
    public @ResponseBody
    void xdthSh(HttpServletRequest req) {
        String sh = req.getParameter("sh");
        String learnid = req.getParameter("learnid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("sh", sh);
        gmap.put("learnid", learnid);
        sjglService.xdthSh(gmap);
    }


    @RequestMapping("xdthPlSh.html")
    public @ResponseBody
    boolean xdthPlSh(HttpServletRequest req) {
        String sh = req.getParameter("sh");
        String learnids = req.getParameter("learnids");
        List<String> listr = (List<String>) JSONArray.parse(learnids);
        for (int m = 0; m < listr.size(); m++) {
            String learnid = listr.get(m).toString();
            Map<String, Object> gmap = new HashMap<String, Object>();
            gmap.put("sh", sh);
            gmap.put("learnid", learnid);
            int i = sjglService.xdthSh(gmap);
            if (i <= 0) {
                return false;
            }
        }
        return true;
    }


    @RequestMapping("Search_bookPlByBid.html")
    @ResponseBody
    public Map<String, Object> Search_bookPlByBid(HttpServletRequest request, HttpServletResponse response) {
        String keyword = "";
        if (request.getParameter("keyword") != null) {
            keyword = request.getParameter("keyword").toString().trim();
        }

        String bid = "";
        if (request.getParameter("bid") != null) {
            bid = request.getParameter("bid").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("keyword", keyword);
        gmap.put("bid", bid);
        List<Map<String, Object>> list1 = sjglService.Search_bookPlByBid(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    @RequestMapping("dicussSh.html")
    public @ResponseBody
    void dicussSh(HttpServletRequest req) {
        String sh = req.getParameter("sh");
        String disid = req.getParameter("disid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("sh", sh);
        gmap.put("disid", disid);
        sjglService.dicussSh(gmap);
    }


    @RequestMapping("dicussPlSh.html")
    public @ResponseBody
    boolean dicussPlSh(HttpServletRequest req) {
        String sh = req.getParameter("sh");
        String disids = req.getParameter("disids");
        List<String> listr = (List<String>) JSONArray.parse(disids);
        for (int m = 0; m < listr.size(); m++) {
            String disid = listr.get(m).toString();
            Map<String, Object> gmap = new HashMap<String, Object>();
            gmap.put("sh", sh);
            gmap.put("disid", disid);
            int i = sjglService.dicussSh(gmap);
            if (i <= 0) {
                return false;
            }
        }
        return true;
    }


}
